package minsat;

/**
 * Simple class which maps a particular literal to a truth value.
 */
public class Binding
{
  public final Literal literal;
  public final boolean binding;

  public Binding(Literal l, boolean b)
  {
    literal = l;
    binding = b;
  }

  public String toString()
  {
    return literal + " <-- " + binding;
  }

  /**
   * Bindings are equal if they share an identical literal and bindings, or if their literals are
   * opposites and their bindings are opposites.
   */
  @Override
  public boolean equals(Object o)
  {
    return literal.equals(((Binding) o).literal) && binding == ((Binding) o).binding
        || literal.equals(new Literal(((Binding) o).literal.name, !((Binding) o).literal.neg))
        && binding != ((Binding) o).binding;
  }

  @Override
  public int hashCode()
  {
    int code = literal.name.hashCode();
    if (literal.neg && binding || !literal.neg && !binding) code++;
    return code;
  }

  public Binding opposite()
  {
    return new Binding(literal, !binding);
  }
}
